import { _decorator, Component, ProgressBar, director, Label, } from 'cc';
import { Login } from '../Cmd/Login';
import { WsMgr } from '../TCP/WsMgr';
import { Config } from '../Common/Config';
const { ccclass, property } = _decorator;
import * as i18n from 'db://i18n/LanguageData';
import { Utils } from '../Common/Utils';
import SVGAPlayer from '../svga-cocos/cocos/svga-player';


@ccclass('LoadMgr')
export default class loadMgr extends Component {



    @property(ProgressBar)
    private progressBar: ProgressBar = null!;

    @property(Label)
    private versionLable: Label = null!;

    @property(Label)
    private loadTip: Label = null!;

    @property(SVGAPlayer)
    firstSVGAPlayer: SVGAPlayer = null; //  播放器

    private passedTime: number = 0.5; //进度条经过的时间
    private totalTime: number = 3;  //完成进度条的时间

    private isLoadPlazaSence: boolean = false;
    private isLoadGameSence: boolean = false;


    private wsSrc: WsMgr;

    protected start(): void {
        // 开始播放第一个动画
        this.firstSVGAPlayer.playSVGA();
    }


    onLoad() {
        this.versionLable.string = Config.VERSION;
        let login = new Login();
        let Request = this.getRequest();
        login.uid = Request["uid"];
        login.token = Request["token"];
        login.language = Request["language"];
        login.liveid = Request["liveid"];
        login.stream = Request["stream"];
        let platform = Request["platform"];
     
        login.platform = platform == null ? "" : platform;
        let joinTableType = Request["joinTableType"];
        let tableType=joinTableType==null?0:joinTableType;
        login.viewingTableId=tableType;

        i18n.init(login.language);

        this.wsSrc = WsMgr.getInstance();
        this.wsSrc.initLoadMgr(this);
        this.wsSrc.saveLoginInfo(login);
        this.wsSrc.saveI18n(i18n);
        this.wsSrc.joinTableType(tableType);

        //http://192.168.31.111:7456/?token=00&uid=10169&language=zh&liveid=10031&stream=10031_1726740988&viewingTableId=0&matchWinNumber=3&uidA=10169&uidB=10159
        // director.preloadScene("plaza", () => {
        //     console.log("plaza scene load success");
        //     this.isLoadPlazaSence = true;
        // })

        director.preloadScene("game", () => {
            console.log("game scene load success");
            this.isLoadGameSence = true;
            if (login.viewingTableId != null) {//如果是观众玩家,等Game场景加载完成之后跳转

            }
        });


    }

    update(dt: number) {

        if (this.progressBar.progress === 1) {
             director.loadScene(Config.SCENE.GAME);
             this.firstSVGAPlayer.clearSvagPlayer();
             this.node.destroy();
            return;
        }

        if (this.passedTime <= 2.7) {
            this.passedTime += dt;
            let BarBgLength: number = this.passedTime / this.totalTime;
            BarBgLength = (BarBgLength > 1) ? 1 : BarBgLength;
            this.progressBar.progress = BarBgLength;
            this.loadTip.string=Math.trunc(BarBgLength*100)+'%'

        }
        if ( this.isLoadGameSence) {//比赛页面
            if (this.passedTime > 2.7&& this.wsSrc.getPlayerInfo() != null) {
                this.passedTime += dt;
                let BarBgLength: number = this.passedTime / this.totalTime;
                BarBgLength = (BarBgLength > 1) ? 1 : BarBgLength;
                this.progressBar.progress = BarBgLength;
                this.loadTip.string=Math.trunc(BarBgLength*100)+'%'
            }
        }


    }



    public getRequest() {//这个函数，其实就是js的方法，在我的博客中能找到出处的
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }





}

